package com.it.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.it.reggie.annotation.Log;
import com.it.reggie.commen.R;
import com.it.reggie.entity.ShoppingCart;
import com.it.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;


@RestController
@RequestMapping("/shoppingCart")
@SuppressWarnings(value = "all")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加购物车
    @Log(value = "用户进行了添加购物车的操作")
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {


        Long userId = (Long) request.getSession().getAttribute("user");

        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

        qw.eq(ShoppingCart::getUserId, userId);

        //在这里判断是菜品还是套餐
        Long dishId = shoppingCart.getDishId();

        if (dishId!=null){

            qw.eq(ShoppingCart::getDishId, dishId);

        }else {

            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        }

        ShoppingCart one = shoppingCartService.getOne(qw);

        //如果是第一次添加则设置数量为1，执行添加操作
        if (one==null){
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);

        }else {
            //如果不是第一次添加，则修改操作数量Number加1
            shoppingCart.setNumber(one.getNumber()+1);

            shoppingCartService.update(shoppingCart,qw);

        }

        return R.success("添加购物车成功");

    }


    //查询购物车
    @GetMapping("/list")
    public R selectAll() {

        List<ShoppingCart> list = shoppingCartService.list();

        for (ShoppingCart shoppingCart : list) {

            Integer number = shoppingCart.getNumber();

            if (number>1){

                BigDecimal amount = shoppingCart.getAmount();

                BigDecimal number1 = new BigDecimal(String.valueOf(BigDecimal.valueOf(number)));

                shoppingCart.setAmount(amount.multiply(number1));

            }

        }


        return R.success(list);

    }

    //取消购物车
    @PostMapping("/sub")
    public R delete(@RequestBody ShoppingCart shoppingCart) {

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(dishId != null, ShoppingCart::getDishId, dishId);
        qw.eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);
        ShoppingCart one = shoppingCartService.getOne(qw);
        Integer number = one.getNumber();
        if (number==1) {
            shoppingCartService.remove(qw);
        }else {
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
        }
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(one);

    }

    //清空购物车
    @DeleteMapping("/clean")
    public R clean(HttpServletRequest request){

        Long userId = (Long) request.getSession().getAttribute("user");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId, userId);

        shoppingCartService.remove(queryWrapper);

        return R.success("删除成功");

    }
}
